热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

C#|类型。GetTypeHandle()方法

C#|类型。GetTypeHandle()方法原文:https

C# |类型。GetTypeHandle()方法

原文:https://www . geesforgeks . org/c-sharp-type-gettypehandle-method/

类型。GetTypeHandle()方法用于获取指定对象类型的句柄。

语法:公共静态 RuntimeTypeHandle GetTypeHandle(对象 o);
在这里,它需要为其获取类型句柄的对象。

返回值:该方法返回指定对象类型的句柄。

异常:如果 o 为空,这个方法抛出 ArgumentNullException

以下程序说明了类型的使用。GetTypeHandle() 方法:

例 1:

// C# program to demonstrate the
// Type.GetTypeHandle() Method
using System;
using System.Globalization;
using System.Reflection;
class GFG {
    // Main Method
    public static void Main()
    {
        // try-catch block for handling Exception
        try {
            // creating and initializing object Type
            Type type = typeof(int);
            // getting handle of given type
            // by using GetTypeHandle() Method
            RuntimeTypeHandle handle = Type.GetTypeHandle(type);
            // Display the Result
            Console.WriteLine("Type referenced is {0}", handle);
        }
        // catch ArgumentNullException here
        catch (ArgumentNullException e) 
        {
            Console.WriteLine("object is null.");
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
    }
}

输出:

Type referenced is System.RuntimeTypeHandle

例 2:

// C# program to demonstrate the
// Type.GetTypeHandle() Method
using System;
using System.Globalization;
using System.Reflection;
class GFG {
    // Main Method
    public static void Main()
    {
        // try-catch block for handling Exception
        try {
            // creating and initializing object Type
            Type type = typeof(int);
            // getting handle of given type
            // by using GetTypeHandle() Method
            RuntimeTypeHandle handle = Type.GetTypeHandle(null);
            // Display the Result
            Console.WriteLine("Type referenced is {0}", handle);
        }
        // catch ArgumentNullException here
        catch (ArgumentNullException e)
        {
            Console.WriteLine("object is null.");
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
    }
}

输出:

object is null.
Exception Thrown: System.ArgumentNullException

参考:


  • https://docs . Microsoft . com/en-us/dotnet/API/system . type . gettypehandle?视图=netframework-4.8


推荐阅读
author-avatar
XIE绍雄
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有